package com.psa.tool.net;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;
import com.google.gson.reflect.TypeToken;
import com.psa.dialog.WaitingDialog;
import com.psa.element.*;
import com.psa.system.R;
import com.psa.tool.IGeneralEventCallback;
import com.psa.tool.SystemStatus;
import com.wxw.BaseLibrary.dialog.SToast;
import com.wxw.BaseLibrary.tool.L;
import com.wxw.BaseLibrary.tool.Timeout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by david on 2015-11-04.
 */
public class DataUpdateManager implements IGeneralEventCallback {
    private List<SqlObjectStatus> for_update = new ArrayList<SqlObjectStatus>();
    private WaitingDialog waitingDialog = null;
    private int itemCount = 0;
    private String message = "";
    private List<DataUpdater> dataUpdaterList = null;

    private Activity parent = null;
    private IGeneralEventCallback onUpdateCompletedListener;
    private IGeneralEventCallback onUpdatingListener;
    private IGeneralEventCallback onUpdateStartedListener;
    private IGeneralEventCallback onUpdateTimeoutListener;


    public DataUpdateManager(Activity activity){
        parent = activity;
    }

    public void setForUpdateItems(List<SqlObjectStatus> for_update) {
        this.for_update = for_update;
    }

    public void setOnUpdateCompletedListener(IGeneralEventCallback onUpdateCompletedListener) {
        this.onUpdateCompletedListener = onUpdateCompletedListener;
    }

    public void setOnUpdatingListener(IGeneralEventCallback onUpdatingListener) {
        this.onUpdatingListener = onUpdatingListener;
    }

    public void setOnUpdateStartedListener(IGeneralEventCallback onUpdateStartedListener) {
        this.onUpdateStartedListener = onUpdateStartedListener;
    }

    public void setOnUpdateTimeoutListener(IGeneralEventCallback onUpdateTimeoutListener) {
        this.onUpdateTimeoutListener = onUpdateTimeoutListener;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void start() {
        if (for_update.size() <= 0){
            SToast.showToast(parent, parent.getString(R.string.update_data_nothing_selected), Toast.LENGTH_SHORT);
            return;
        }
        itemCount = for_update.size();
        dataUpdaterList = new ArrayList<DataUpdater>();
        if (onUpdateStartedListener != null) {
            onUpdateStartedListener.finished(this, "Started");
        }
        waitingDialog = new WaitingDialog(parent, parent.getString(R.string.app_sys_info), message, 10*itemCount, new Timeout.OnTimeoutListener() {
            @Override
            public void onTimeout(Object sender, int time) {
//                if (dataUpdaterList.size()>0){
//                    for(DataUpdater du : dataUpdaterList)
//                        du.cancel();
//                }
                if (waitingDialog != null) {
                    waitingDialog.stop();
                }
                if (onUpdateTimeoutListener != null) {
                    onUpdateTimeoutListener.finished(DataUpdateManager.this, parent.getString(R.string.update_data_timeout));
                }
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (SqlObjectStatus item : for_update){
                        DataUpdater dataUpdater = new DataUpdater(item.type, item.iSqlObject);
                        dataUpdater.setOnUpdateCompletedListener(DataUpdateManager.this);
                        dataUpdater.setOnUpdatingListener(onUpdatingListener);
                        dataUpdater.setClearDB(true);
                        dataUpdater.getBaseDataByPost();
                        dataUpdaterList.add(dataUpdater);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(SystemStatus.TAG, e.getMessage());
                }
            }
        }).start();
    }

    @Override
    public void finished(Object sender, String eventContent) {
        try {
            itemCount--;
            try {
                dataUpdaterList.remove(sender);
            } catch (Exception e) {
                Log.e(SystemStatus.TAG, e.getMessage());
                L.WriteExceptionLog("DataUpdaterManager", "finished", e.getMessage());
            }
//            if (sender instanceof DataUpdater && ((DataUpdater) sender).getType().toString().equals(new TypeToken<List<MerchandiseSeq>>() {
//            }.getType().toString())) {
//                try {
//                    SystemStatus.fullDefaultMerchandise();
//                } catch (Exception ex){
//                    Log.e(SystemStatus.TAG, ex.getMessage());
//                }
//            }
//            if (sender instanceof DataUpdater && ((DataUpdater) sender).getType().toString().equals(new TypeToken<List<Customer>>() {
//            }.getType().toString())) {
//                try {
//                    SystemStatus.currentCustomers = ((DataUpdater) sender).getData();
//                } catch (Exception ex) {
//                    Log.e(SystemStatus.TAG, ex.getMessage());
//                }
//            }

//            if (onUpdatingListener != null)
//                onUpdatingListener.finished(sender, eventContent);
            if (itemCount <= 0) {
                String s = parent.getString(R.string.update_data_complated);
                waitingDialog.stop();
                waitingDialog = null;
                if (onUpdateCompletedListener != null) {
                    onUpdateCompletedListener.finished(this, s);
                }
            }
        } catch (Exception e) {
            Log.e(SystemStatus.TAG, e.getMessage());
            L.WriteExceptionLog("DataUpdaterManager", "finished:1", e.getMessage());
        }
    }

    public static List<SqlObjectStatus> createUpdateItems(){
        List<SqlObjectStatus> items = new ArrayList<SqlObjectStatus>();
        SqlObjectStatus sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new Customer();
        sqlObjectStatus.type = new TypeToken<List<Customer>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item1);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new OperatorCustomer();
        sqlObjectStatus.type = new TypeToken<List<OperatorCustomer>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item12);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new Distributor();
        sqlObjectStatus.type = new TypeToken<List<Distributor>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item14);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new MerchandisePrice();
        sqlObjectStatus.type = new TypeToken<List<MerchandisePrice>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item15);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new TradeMode();
        sqlObjectStatus.type = new TypeToken<List<TradeMode>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item2);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new Channel();
        sqlObjectStatus.type = new TypeToken<List<Channel>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item3);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new Department();
        sqlObjectStatus.type = new TypeToken<List<Department>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item4);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new Merchandise();
        sqlObjectStatus.type = new TypeToken<List<Merchandise>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item5);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new District();
        sqlObjectStatus.type = new TypeToken<List<District>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item6);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new MerchandiseSeq();
        sqlObjectStatus.type = new TypeToken<List<MerchandiseSeq>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item7);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new Brand();
        sqlObjectStatus.type = new TypeToken<List<Brand>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item8);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new com.psa.element.Package();
        sqlObjectStatus.type = new TypeToken<List<com.psa.element.Package>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item9);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new com.psa.element.PDA_Defdoc();
        sqlObjectStatus.type = new TypeToken<List<com.psa.element.PDA_Defdoc>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item16);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new com.psa.element.PDA_DefdocList();
        sqlObjectStatus.type = new TypeToken<List<com.psa.element.PDA_DefdocList>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item17);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new com.psa.element.Showcase();
        sqlObjectStatus.type = new TypeToken<List<com.psa.element.Showcase>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item18);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);
        sqlObjectStatus = new SqlObjectStatus();
        sqlObjectStatus.iSqlObject = new com.psa.element.CustomerShowcase();
        sqlObjectStatus.type = new TypeToken<List<com.psa.element.CustomerShowcase>>(){}.getType();
        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item19);
        sqlObjectStatus.checked = true;
        items.add(sqlObjectStatus);

//        sqlObjectStatus = new SqlObjectStatus();
//        sqlObjectStatus.iSqlObject = new OrderInvoice();
//        ((OrderInvoice)sqlObjectStatus.iSqlObject).Days = 7;
//        sqlObjectStatus.type = new TypeToken<List<OrderInvoice>>(){}.getType();
//        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item10);
//        items.add(sqlObjectStatus);
//        sqlObjectStatus = new SqlObjectStatus();
//        sqlObjectStatus.iSqlObject = new OrderInvoiceItem();
//        ((OrderInvoiceItem)sqlObjectStatus.iSqlObject).Days = 7;
//        sqlObjectStatus.type = new TypeToken<List<OrderInvoiceItem>>(){}.getType();
//        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item11);
//        items.add(sqlObjectStatus);
//        sqlObjectStatus = new SqlObjectStatus();
//        sqlObjectStatus.iSqlObject = new Interchange();
//        sqlObjectStatus.type = new TypeToken<List<Interchange>>(){}.getType();
//        sqlObjectStatus.title = SystemStatus.MainActivity.getString(R.string.update_data_item13);
//        items.add(sqlObjectStatus);
        return items;
    }
}
